package app.dws;

import app.BaseAppV1;
import beans.TradeSourceOrderBean;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import common.Constant;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.functions.ReduceFunction;
import org.apache.flink.api.common.state.StateTtlConfig;
import org.apache.flink.api.common.state.ValueState;
import org.apache.flink.api.common.state.ValueStateDescriptor;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.streaming.api.functions.windowing.ProcessWindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;
import org.apache.flink.util.FlinkUserCodeClassLoader;
import utils.AtguiguUtil;
import utils.FlinkSinkUtil;

import java.time.Duration;
import java.util.HashSet;

public class Dws_0401_DwsTradeSourceOrderWindow extends BaseAppV1 {
    public static void main(String[] args) {
        new Dws_0401_DwsTradeSourceOrderWindow().init(
                3401,2,
                "Dws_0401_DwsTradeSourceOrderWindow",
                Constant.TOPIC_DWD_TRADE_WITH_SOURCE

        );
    }
    @Override
    protected void handle(StreamExecutionEnvironment env, DataStreamSource<String> stream) {
        stream.
                map(JSON::parseObject)
                .keyBy(obj->obj.getString("user_id"))
                .process(new KeyedProcessFunction<String, JSONObject, TradeSourceOrderBean>() {

                    private ValueState<String> lastStateDate;

                    @Override
                    public void open(Configuration parameters) throws Exception {
                        ValueStateDescriptor<String> desc = new ValueStateDescriptor<>("lastStateDate", String.class);
                        desc.enableTimeToLive(new StateTtlConfig.Builder(org.apache.flink.api.common.time.Time.seconds(20)).build());
                        lastStateDate = getRuntimeContext().getState(desc);
                    }

                    @Override
                    public void processElement(JSONObject value,
                                               Context ctx,
                                               Collector<TradeSourceOrderBean> out) throws Exception {
                        long ts = value.getLong("ts")*1000;
                        String today = AtguiguUtil.toDate(ts);
                        String lastState = lastStateDate.value();
                        long uv =0L;

                        HashSet<String> userIdSet = new HashSet<>();
                        userIdSet.add(value.getString("user_id"));

                        HashSet<String> orderIdset = new HashSet<>();
                        orderIdset.add(value.getString("order_id"));

                        Long finalAmount = value.getLong("final_amount");
                        String sc = value.getString("sc");
                        if (!today.equals(lastState)) {
                            uv = 1L;
                        }

                        TradeSourceOrderBean bean = new TradeSourceOrderBean("", "",
                                userIdSet, 0L, orderIdset, 0L, sc, "", finalAmount, uv, ts);
                        out.collect(bean);
                    }
                })
                .assignTimestampsAndWatermarks(WatermarkStrategy
                        .<TradeSourceOrderBean>forBoundedOutOfOrderness(Duration.ofSeconds(3))
                        .withTimestampAssigner((bean,ts)->bean.getTs())
                )
                .keyBy(TradeSourceOrderBean::getSc)
                .window(TumblingEventTimeWindows.of(Time.seconds(5)))
                .reduce(new ReduceFunction<TradeSourceOrderBean>() {
                            @Override
                            public TradeSourceOrderBean reduce(TradeSourceOrderBean value1, TradeSourceOrderBean value2) throws Exception {
                                value1.getUserIdSet().addAll(value2.getUserIdSet());
                                value1.getOrderIdSet().addAll(value2.getOrderIdSet());
                                value1.setFinalAmount(value1.getFinalAmount()+value2.getFinalAmount());
                                value1.setUv(value1.getUv()+value2.getUv());
                                return value1;
                            }
                        },
                        new ProcessWindowFunction<TradeSourceOrderBean, TradeSourceOrderBean, String, TimeWindow>() {
                            @Override
                            public void process(String s, Context context, Iterable<TradeSourceOrderBean> elements, Collector<TradeSourceOrderBean> out) throws Exception {
                                TradeSourceOrderBean bean = elements.iterator().next();
                                bean.setStt(AtguiguUtil.toDatTime(context.window().getStart()));
                                bean.setEdt(AtguiguUtil.toDatTime(context.window().getEnd()));

                                bean.setUserCount((long) bean.getUserIdSet().size());
                                bean.setOrderCount((long) bean.getOrderIdSet().size());

                                bean.setTs(System.currentTimeMillis());
                            }
                        }
                )
                .addSink(FlinkSinkUtil.getClickHouseSink("dws_trade_source_order_window",TradeSourceOrderBean.class));
    }
}
